package cn.sumpu.app.coolsk.biz.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.coolsk.biz.CompanyOrderBiz;
import cn.sumpu.app.coolsk.constant.OrderStatus;
import cn.sumpu.app.coolsk.constant.PageSize;
import cn.sumpu.app.coolsk.constant.ProductType;
import cn.sumpu.app.coolsk.dao.CompanyOrderDao;
import cn.sumpu.app.coolsk.domain.CompanyOrder;
import cn.sumpu.app.coolsk.domain.Order;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.app.coolsk.util.ExportExcel;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

public class CompanyOrderBizImpl implements CompanyOrderBiz {
	private final static Logger logger = LoggerFactory
			.getLogger(CompanyOrderBizImpl.class);
	private CompanyOrderDao companyOrderDao;

	public void setCompanyOrderDao(CompanyOrderDao companyOrderDao) {
		this.companyOrderDao = companyOrderDao;
	}

	@Override
	public PageModel getCompanyOrder(Integer page, String company,String ticket,
			String beginDate, String endDate, String orderType)
			throws AppBusinessException {
		try {
			int pageSize = PageSize.PAGE_SIZE_COMPANY_ORDER;
			page = page == null ? 1 : page;// page默认为1
			orderType = orderType == null ? ProductType.TICKET.getValue()
					: orderType;// 默认查门票
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if (!Common.empty(company)) {
				map.put("company", company);
				if (!Common.empty(ticket)) {
					map.put("ticket", ticket);
				}
			}
			if (!Common.empty(beginDate)) {
				map.put("beginDate", format.parse(beginDate));
			}
			if (!Common.empty(endDate)) {
				map.put("endDate", format.parse(endDate));
			}
			if (!Common.empty(orderType)) {
				map.put("type", orderType);
				if (orderType.equals(ProductType.TICKET.getValue())) {
					map.put("status", OrderStatus.BILLED.getValue());
				}
			}
			int count = companyOrderDao.getCompanyOrderCount(map); 
			List<Order> list = companyOrderDao.getCompanyOrder(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(list);
			pm.setPageSize(pageSize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pageSize - 1) / pageSize);
			return pm;
		} catch (Exception e) {
			return null;
		}

	}

	@Override
	public HSSFWorkbook exportExcel(String company, String beginDate, String endDate,
			String orderType) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!Common.empty(company)) {
			map.put("company", company);
		}
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if (!Common.empty(beginDate)) {
				map.put("beginDate", format.parse(beginDate));
			}
			if (!Common.empty(endDate)) {
				map.put("endDate", format.parse(endDate));
			}
			if (!Common.empty(orderType)) {
				map.put("type", orderType);
				if (orderType.equals(ProductType.TICKET.getValue())) {
					map.put("status", OrderStatus.BILLED.getValue());
				}
			}
			List<Order> listOrder = companyOrderDao.getCompanyOrder(map); 
			if (!Common.empty(listOrder) && listOrder.size() > 0) {
				List<CompanyOrder> list = new ArrayList<CompanyOrder>();
				for (Order order : listOrder) {
					CompanyOrder co = new CompanyOrder(order.getPname(),order.getId(),order.getContactor(),order.getOrderDetail(),order.getStampNum(),order.getOrderTime());
					list.add(co);
				}
				ExportExcel<CompanyOrder> ex = new ExportExcel<CompanyOrder>();
				if (orderType.equals(ProductType.TICKET.getValue())) {
					String[] headers = { "门票名","订单号", "取票人", "订单详情", "票号", "游玩日期","签名"};
					return ex.createExcel("门票订单", headers, list, "yyyy-MM-dd");
				} else if (orderType.equals(ProductType.HOTEL.getValue())) {
					String[] headers = { "订单号", "预定人", "订单详情", "票号", "入住日期","签名" };
					return ex.createExcel("酒店订单", headers, list, "yyyy-MM-dd");
				} else if (orderType.equals(ProductType.PRODUCT.getValue())) {
					String[] headers = { "订单号", "订购人", "订单详情", "票号", "订购日期","签名" };
					return ex.createExcel("产品订单", headers, list, "yyyy-MM-dd");
				} else {
					return null;
				}
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (logger.isErrorEnabled()) {
				logger.error("exportExcel failure.", e);
			}
			return null;
		}

	}

}
